# booru/forms.py
from django import forms
from .models import Image
from django.core.exceptions import ValidationError
from .models import User


class ImageUploadForm(forms.ModelForm):
    class Meta:
        model = Image
        fields = ['title', 'description', 'image_file']
        widgets = {
            'description': forms.Textarea(attrs={'rows': 3}),
        }

class RegisterForm(forms.Form):
    username = forms.CharField(max_length=64, label="用户名")
    password = forms.CharField(widget=forms.PasswordInput, label="密码")
    password_confirm = forms.CharField(widget=forms.PasswordInput, label="确认密码")

    def clean_username(self):
        username = self.cleaned_data['username']
        if User.objects.filter(username=username).exists():
            raise ValidationError("用户名已存在")
        return username

    def clean(self):
        cleaned_data = super().clean()
        if cleaned_data.get('password') != cleaned_data.get('password_confirm'):
            raise ValidationError("两次密码输入不一致")
        return cleaned_data

class LoginForm(forms.Form):
    username = forms.CharField(max_length=64, label="用户名")
    password = forms.CharField(widget=forms.PasswordInput, label="密码")
